<?php defined('BASEPATH') or exit('No direct script access allowed');

class Registration_m extends MY_Model {
	function __construct(){
		parent::__construct();
	}
	
	function submit_user_step1(){
		$email 				= $this->input->post('email',TRUE);
		$username 			= $this->input->post('username',TRUE);
		$name 				= $this->input->post('name',TRUE);
		$phone 				= $this->input->post('phone',TRUE);
		$address			= $this->input->post('address',TRUE);
		$captcha			= $this->input->post('captcha',TRUE);
		$category_id		= $this->input->post('category_id', TRUE);
		$country_id 		= $this->input->post('country_id',TRUE);
		$state_id 			= $this->input->post('state_id',TRUE);
		$province_id 		= $this->input->post('province_id',TRUE);
		$city_id			= $this->input->post('city_id', TRUE);
		$commercial_id		= $this->input->post('commercial_id', TRUE);
		$borncountry_id 		= $this->input->post('borncountry_id',TRUE);
		
		$correctCaptcha 	= $this->session->userdata('captcha_content');
		$expTime			= $this->session->userdata('captcha_exprise_time');
		$captcha = strtolower($captcha);
		
		if( ! $this->phpvalidator->is_alphanumericdash($username) ){
			echo "username_err|".language_translate('registration_m_username_err2');
			exit();
		}
		if( ! $this->phpvalidator->is_email($email) || strlen($email) > 45){
			echo "email_err|".language_translate('registration_m_email_err1');
			exit();	
		}
	 	if( $correctCaptcha != $captcha){
			echo "captcha_err|".language_translate('registration_m_captcha_err1');
			exit();
		}
	 	if( time() > $expTime ){
			echo "captcha_err|".language_translate('registration_m_captcha_err2');
			exit();
		}
		
		$USER = $this->user_entity->init('username',$username);
		if($USER){
			echo "username_err|".language_translate('registration_m_username_err');
			exit();
		}
		$USER = $this->user_entity->init('email',$email);
		if($USER){
			echo "email_err|".language_translate('registration_m_email_err2');
			exit();
		}
		
		$newId = $this->user_entity->createNewUserEntity();
		$USER = $this->user_entity->init('id', $newId);
		$USER->setUserName($username);
		$USER->setEmail($email);
		$USER->setPassword($this->digit->rand_digit(6));
		$USER->setName($name);
		$USER->setPermissionSlug(USER_TYPE_ESCORT);
		$USER->setAddress($address);
		$USER->setPhone($phone);
		$USER->setCreateTime(time());
		$USER->setCommission(0);
		$USER->setCountryId($country_id);
		$USER->setSiteRef(SITE_REF);
		$USER->setStateId($state_id);
		$USER->setProvinceId($province_id);
		$USER->setCityId($city_id);
		$USER->setStatus('pending');
		$USER->update();
		
		$id = $this->escort_entity->createNewEscortEntity();
		$ESCORT = $this->escort_entity->init('id',$id);
		$ESCORT->setCommericalLead($commercial_id);
		$ESCORT->setUserId($newId);
		$ESCORT->setCountryId($country_id);
		$ESCORT->setStateId($state_id);
		$ESCORT->setProvinceId($province_id);
		$ESCORT->setCityId($city_id);
		$ESCORT->setEscortCategoryId($category_id);
		$ESCORT->setEscortName($name);
		$ESCORT->setEscortEmail($email);
		$ESCORT->setEscortAddress($address);
		$ESCORT->setEscortPhone($phone);
		$ESCORT->setBornCountryId($borncountry_id);
		$ESCORT->update();
		
		$this->email_sender->sendEmailToUser_escortRegister($newId);
		$this->action_log->insertNewAction($USER->getId(),'log_action_register');
		
		echo 'ok';exit();
	}
	
	function activated_account(){
		$code = $this->input->get('code', TRUE);
		$USER = $this->user_entity->init('activated_code', $code);
		if(!$USER){
			show_404();
		}
		if($USER->getStatus() != 'pending'){
			show_404();
		}
		$USER->setStatus('activated'); 
		$USER->update();
		
		$ESCORT = $this->escort_entity->init('user_id',$USER->getId());
		$ESCORT->setPublished(0);
		$ESCORT->update();
		
		$session_array = array('account'=> array('user_id'=>$USER->getId() ,'user_obj'=>$USER) );
		$this->session->set_userdata($session_array);	
		redirect("registration/services");
	}
	
	function submit_user_contract(){
		$subscription_id = $this->input->post('subscription', TRUE);
		$sexual_orientation = $this->input->post('sexual_orientation', TRUE);
		$list_place = implode(',', $_POST['list_place']);
		$list_pack = implode(',', $_POST['list_pack']);
		$promotion_code = $this->input->post('promotion_code', TRUE);
		$payment_type = $this->input->post('payment_type', TRUE);
		$cardname = $this->input->post('cardname', TRUE);
		$creditcard = $this->input->post('creditcard', TRUE);
		$month = $this->input->post('month', TRUE);
		$year = $this->input->post('year', TRUE);
		$cvv = $this->input->post('cvv', TRUE);
		
		$USER = getAccountUserObject();
		$ESCORT = $this->escort_entity->init('user_id',$USER->getId());
		$ESCORT->setSubscriptionId($subscription_id);
		$ESCORT->setPlacesListIdsContract($list_place);
		$ESCORT->setSexualOrientationId($sexual_orientation);
		$ESCORT->setPackageIds($list_pack);
		$ESCORT->setPromotionCode($promotion_code);
		$ESCORT->update();
		$this->email_sender->sendEmailToUser_newContractCreated($USER->getId());
		
		$this->action_log->insertNewAction($USER->getId(),'log_action_contract');
		echo 'ok';
	}
	
	
	
	
	
	
	
	
	
	
	function changeCountry_async(){
		$country_id = $this->input->post('country_id',0);
		$stateArray = getStateData_ioc($country_id);
		 
		$html = form_dropdown('state_id', $stateArray, array() , 'id="state_id"' );
		echo $html;
	}
	
	function changeState_async(){
		$state_id = $this->input->post('state_id',0);
		$country_id = $this->input->post('country_id',0);
		$provinceArray = getProvinceData_ioc($country_id,$state_id);
		 
		$html = form_dropdown('province_id', $provinceArray, array() , 'id="province_id"' );
		echo $html;
	}
	
	function changeProvince_async(){
		$state_id = $this->input->post('state_id',0);
		$province_id = $this->input->post('province_id',0);
		$country_id = $this->input->post('country_id',0);
		$cityArray = getCityData_ioc($country_id,$state_id,$province_id);
		
		$html = form_dropdown('city_id', $cityArray, array() , 'id="city_id"' );
		echo $html;
	}
		
	function calculatePriceContract_asysn(){
		$subscription_id = $this->input->post('subscription_id', TRUE);
		$list_place_ids = $this->input->post('list_place_ids', TRUE);
		$list_pack_ids = $this->input->post('list_pack_ids', TRUE);
		$list_priority_id = $this->input->post('list_priority_id', TRUE);
		
		$place_array = array_filter( explode(',',$list_place_ids) );
		$pack_array = array_filter( explode(',',$list_pack_ids) );
		$priority_array = array_filter( explode(',',$list_priority_id) );
		
		$subscriptionDataArray = getSubcriptionData_ioc();
		$placesDataArray = getPlaceListWorkData_ioc();
		$packDataArray = getPackListData_ioc();
		$priorityArray = getPriorityData_ioc();
		
		$price = 0;
		if($subscription_id){
			$price += $subscriptionDataArray[$subscription_id][1];
		}
		
		if(!empty($place_array)){
			foreach($place_array as $item){
				if($item){
					$price += $placesDataArray[$item][1];
				}
			}
		}
		if(!empty($pack_array)){
			foreach($pack_array as $item){
				if($item){
					$price += $packDataArray[$item][1];
				}
			}
		}
		if(!empty($priority_array)){
			foreach($priority_array as $item){
				if($item){
					$price += $priorityArray[$item][1];
				}
			}
		}
		echo currencyDisplay($price). ' '.language_translate('services_page_euro_monthly');
	}	
		
	
		
		
		
		
		
		
		
//end class	 
}